package com.mask.token.service;

import com.mask.token.model.MaskUserDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * 默认用户详情服务
 * 用户需要实现自己的UserDetailsService来替换此默认实现
 *
 * @author mask
 * @since 1.0.0
 */
@Slf4j
@Service
@ConditionalOnMissingBean(UserDetailsService.class)
public class MaskUserDetailsService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.warn("使用默认的UserDetailsService，请实现自己的UserDetailsService");
        
        // 默认实现，实际使用时应该被用户自定义的UserDetailsService替换
        MaskUserDetails userDetails = new MaskUserDetails();
        userDetails.setUserId(1L);
        userDetails.setUsername(username);
        userDetails.setPassword("$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iKTVEFDi"); // 默认密码: 123456
        userDetails.setAuthorities(Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")));
        userDetails.setEnabled(true);
        userDetails.setAccountNonExpired(true);
        userDetails.setAccountNonLocked(true);
        userDetails.setCredentialsNonExpired(true);
        
        return userDetails;
    }
}
